//
//  SHQRCodeImageView.m
//  YHSJ
//
//  Created by CYF on 16/11/23.
//  Copyright © 2016年 SH. All rights reserved.
//
#import "UIImage+CornerRadius.h"

#import "SHQRCodeImageView.h"

@implementation SHQRCodeImageView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
        
    return  self;
    
}

#pragma mark * 生成二维码
- (UIImage *)createNormalERImageView{

    //制定规格
    NSString *string = self.qrCodeString;
    CGSize size = CGSizeMake(self.frame.size.width, self.frame.size.height);
    
    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
    //创建一个过滤器类
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    
    NSLog(@"qrFilter --> %@",qrFilter);
    CIImage *qrImage = qrFilter.outputImage;
    //放大并绘制二维码 (上面生成的二维码很小,需要放大)
    CIContext *CIcontext = [CIContext contextWithOptions:nil];
    NSLog(@"qrImage --> %@",qrImage);
    //调用上下文和提供的CIImage 生成 CGImageRef
    CGImageRef cgImage = [CIcontext createCGImage:qrImage fromRect:qrImage.extent];
    //新创建的上下文大小
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    //翻转图片,否则生成的QRCode是上下颠倒的
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    
    return codeImage;
}
- (UIImage *)createColorERImageView:(UIColor *)backColor frontColor:(UIColor *)frontColor{
    
    UIImage *image = [self createNormalERImageView];
    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:
                             @"inputImage",[CIImage imageWithCGImage:image.CGImage],
                             @"inputColor0",[CIColor colorWithCGColor:frontColor.CGColor],
                             @"inputColor1",[CIColor colorWithCGColor:backColor.CGColor],
                             nil];
    
    return [UIImage imageWithCIImage:colorFilter.outputImage];
}


- (UIImage *)createNormalSmallImageERImageViewSmallImgSize:(CGSize)smallSize samllImgWhiteBgSize:(CGSize)whiteBgSize{
    UIImage *image = [self createSmallImageERImageView:[self createNormalERImageView] SmallImgSize:smallSize samllImgWhiteBgSize:whiteBgSize];
   
    return image;
}

- (UIImage *)createColorSmallImageERImageView:(UIColor *)backColor frontColor:(UIColor *)frontColor smallImgSize:(CGSize)smallSize samllImgWhiteBgSize:(CGSize)whiteBgSize{
    UIImage *qrImage = [self createColorERImageView:backColor frontColor:frontColor];
    UIImage *image = [self createSmallImageERImageView:qrImage SmallImgSize:smallSize samllImgWhiteBgSize:whiteBgSize];
    
    return image;
}

- (UIImage *)createSmallImageERImageView:(UIImage *)qrImage SmallImgSize:(CGSize)smallSize samllImgWhiteBgSize:(CGSize)whiteBgSize{
    UIGraphicsBeginImageContextWithOptions(qrImage.size, NO,2);//使用此方法发解决生成的二维码变模糊问题

    //UIGraphicsBeginImageContext(qrImage.size);;//根据当前大小创建一个基于位图图形的环境
    [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];
    
    UIImage *image = [UIImage imageNamed:self.qrCodeSmallImage];
    
    CGFloat imageW = smallSize.width;
    CGFloat imageX = (qrImage.size.width - imageW) * 0.5;
    CGFloat imgaeY = (qrImage.size.height - imageW) * 0.5;
    CGFloat bgImageW = whiteBgSize.width;
    CGFloat bgImageX = (qrImage.size.width - bgImageW) * 0.5;
    CGFloat bgImageY = (qrImage.size.height - bgImageW) * 0.5;
     
    CGRect rect = CGRectMake(bgImageX  ,bgImageY  , bgImageW,  bgImageW);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextFillRect(context, rect);
    [image drawInRect:CGRectMake(imageX, imgaeY, imageW, imageW)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
       
    return result;
}


@end
